package com.palnar.ilecker.adapter;

import java.util.ArrayList;

import com.palnar.ilecker.R;
import com.palnar.ilecker.model.adapterModels.WaiterTableModel;
import com.palnar.ilecker.utils.Utils;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import android.widget.RelativeLayout;

public class TableGridAdapter extends ArrayAdapter<WaiterTableModel> {

	Context context;

	private RelativeLayout.LayoutParams layoutSize;

	public TableGridAdapter(Context context, int resource,
			ArrayList<WaiterTableModel> objects, int numColumns) {
		super(context, resource, objects);

		this.context = context;

		int width = (Utils.getDeviceWidth(context) / numColumns);
		layoutSize = new RelativeLayout.LayoutParams(width, width);

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		final WaiterTableModel rowItem = getItem(position);

		LayoutInflater mInflater = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_waiter_table, null);
			holder = new ViewHolder();

			holder.layout = (RelativeLayout) convertView
					.findViewById(R.id.relItem);
			holder.layTableFirst = (LinearLayout) convertView
					.findViewById(R.id.layTableFirst);

			holder.layTableSecond = (LinearLayout) convertView
					.findViewById(R.id.layTableSecond);

			holder.txtT1 = (TextView) convertView.findViewById(R.id.txtT1);
			holder.txtT2 = (TextView) convertView.findViewById(R.id.txtT2);
			holder.txtT3 = (TextView) convertView.findViewById(R.id.txtT3);
			holder.txtT4 = (TextView) convertView.findViewById(R.id.txtT4);

			holder.btnMore = (ImageView) convertView.findViewById(R.id.btnMore);

			convertView.setTag(holder);
		} else
			holder = (ViewHolder) convertView.getTag();

		/*
		 * holder.txtDesc.setText(rowItem.getDesc());
		 * holder.txtTitle.setText(rowItem.getTitle());
		 * holder.imageView.setImageResource(rowItem.getImageId());
		 */
		holder.layout.setLayoutParams(layoutSize);

		if (rowItem.getTableName().length == 1) {
			holder.layTableFirst.setVisibility(View.VISIBLE);
			holder.layTableSecond.setVisibility(View.GONE);

			holder.txtT1.setVisibility(View.VISIBLE);
			holder.txtT2.setVisibility(View.GONE);
			holder.txtT3.setVisibility(View.GONE);
			holder.txtT4.setVisibility(View.GONE);

			holder.txtT1.setText(rowItem.getTableName()[0]);
			holder.txtT1.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[0]));

		} else if (rowItem.getTableName().length == 2) {
			holder.layTableFirst.setVisibility(View.VISIBLE);
			holder.layTableSecond.setVisibility(View.VISIBLE);

			holder.txtT1.setVisibility(View.VISIBLE);
			holder.txtT2.setVisibility(View.GONE);
			holder.txtT3.setVisibility(View.VISIBLE);
			holder.txtT4.setVisibility(View.GONE);

			holder.txtT1.setText(rowItem.getTableName()[0]);
			holder.txtT1.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[0]));

			holder.txtT3.setText(rowItem.getTableName()[1]);
			holder.txtT3.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[1]));

		} else if (rowItem.getTableName().length == 3) {
			holder.layTableFirst.setVisibility(View.VISIBLE);
			holder.layTableSecond.setVisibility(View.VISIBLE);

			holder.txtT1.setVisibility(View.VISIBLE);
			holder.txtT2.setVisibility(View.VISIBLE);
			holder.txtT3.setVisibility(View.VISIBLE);
			holder.txtT4.setVisibility(View.GONE);

			holder.txtT1.setText(rowItem.getTableName()[0]);
			holder.txtT1.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[0]));

			holder.txtT2.setText(rowItem.getTableName()[1]);
			holder.txtT2.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[1]));

			holder.txtT3.setText(rowItem.getTableName()[2]);
			holder.txtT3.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[2]));

		} else if (rowItem.getTableName().length == 4) {
			holder.layTableFirst.setVisibility(View.VISIBLE);
			holder.layTableSecond.setVisibility(View.VISIBLE);

			holder.txtT1.setVisibility(View.VISIBLE);
			holder.txtT2.setVisibility(View.VISIBLE);
			holder.txtT3.setVisibility(View.VISIBLE);
			holder.txtT4.setVisibility(View.VISIBLE);

			holder.txtT1.setText(rowItem.getTableName()[0]);
			holder.txtT1.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[0]));

			holder.txtT2.setText(rowItem.getTableName()[1]);
			holder.txtT2.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[1]));

			holder.txtT3.setText(rowItem.getTableName()[2]);
			holder.txtT3.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[2]));

			holder.txtT4.setText(rowItem.getTableName()[3]);
			holder.txtT4.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[3]));

		} else if (rowItem.getTableName().length > 4) {
			holder.layTableFirst.setVisibility(View.VISIBLE);
			holder.layTableSecond.setVisibility(View.VISIBLE);

			holder.txtT1.setVisibility(View.VISIBLE);
			holder.txtT2.setVisibility(View.VISIBLE);
			holder.txtT3.setVisibility(View.VISIBLE);
			holder.txtT4.setVisibility(View.VISIBLE);

			holder.txtT1.setText(rowItem.getTableName()[0]);
			holder.txtT1.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[0]));

			holder.txtT2.setText(rowItem.getTableName()[1]);
			holder.txtT2.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[1]));

			holder.txtT3.setText(rowItem.getTableName()[2]);
			holder.txtT3.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[2]));

			holder.txtT4.setText("...");
			holder.txtT4.setBackgroundColor(context.getResources().getColor(
					rowItem.getTableColor()[3]));

		}

		holder.btnMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				showPopupMenu(v);

			}
		});

		return convertView;
	}

	private void showPopupMenu(View v) {
		PopupMenu popupMenu = new PopupMenu(context, v);
		popupMenu.getMenuInflater().inflate(R.menu.waiter_menu,
				popupMenu.getMenu());

		popupMenu
				.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

					@Override
					public boolean onMenuItemClick(MenuItem item) {
						Toast.makeText(context, item.toString(),
								Toast.LENGTH_LONG).show();
						return true;
					}
				});

		popupMenu.show();
	}

	/* private view holder class */
	private class ViewHolder {

		RelativeLayout layout;
		LinearLayout layTableFirst;
		LinearLayout layTableSecond;

		TextView txtT1;
		TextView txtT2;
		TextView txtT3;
		TextView txtT4;

		ImageView btnMore;
	}

}
